package main

import (
	"log"
	"os"
	"project/bmtool/src/mecro"
	"project/bmtool/src/mysql"
)

var ILogger = log.New(os.Stdout, "[INFO]", log.LstdFlags|log.Lshortfile)
var WLogger = log.New(os.Stdout, "[WARNING]", log.LstdFlags|log.Lshortfile)
var ELogger = log.New(os.Stdout, "[ERROR]", log.LstdFlags|log.Lshortfile)

func main() {

	args := os.Args[1:]
	if len(args) < 3 {
		ELogger.Print(`
		=============================== USAGE ===============================
		Plase use: bmtool ${gen type} ...
		    bmtool : This executable program
			${gen type}: gen type db 数据库处理 mecro类似C++的宏生成函数, EX. db/mecro
		gen type is db:
		    usage: bmtool db ${sql_file} ${sql dirver name} ${package name} [${true/false}] [${work dir}] [${work package name}]
		    ${sql_file}            : SQL script file directory, EX. ../test.sql
		    ${sql dirver name}     : SQL dirver name, EX. mysql/sqlite
		    ${package name}        : generate golang package name
		    [${true/false}]        : Whether to generate a business logic operation
		    [${work dir}]          : business logic code golang save to directory
		    [${work package name}] : business logic golang package name
		gen type is mecro:
			usage: bmtool mecro ${packagename} ${func name} ${data struct name} ${db func name} [${true/false}]
			${packagename}         : 包名
			${func name}           : 要生成的函数名
			${data struct name}    : 操作数据名
			${db func name}        : 数据库操作的函数名
			[${true/false}]        : 该参数表示生成的是批量操作与否，不传表示false
	
		if the fourth param is true
		work dir is gen work code directory
		Arguments to brackets may not be passed
		================================ END ================================`)

		ELogger.Fatalln("cmd line args error")
	}

	genType := args[0]

	switch genType {
	case "db":
		sqlFile := args[1]
		sqlDirver := args[2]
		packageName := args[3]
		genWork := false
		workDir := "./"
		workPackage := "default"

		if len(args) > 4 {
			tmp := args[4]
			if tmp == "true" {
				genWork = true
			}
		}
		if len(args) > 5 {
			workDir = args[5]
		}
		if len(args) > 6 {
			workPackage = args[6]
		}
		if sqlDirver == "mysql" {
			mysql.ProcessSql(sqlFile, packageName, genWork, workDir, workPackage)
		}
	case "mecro":
		pname := args[1]
		fname := args[2]
		stName := args[3]
		dbfname := args[4]
		multiOpt := "false"
		if len(args) > 5 {
			multiOpt = args[5]
		}
		mecro.GenCode(pname, fname, stName, dbfname, multiOpt)
	}

}
